// WarehouseSlotContainer.cpp: implementation of the CWarehouseSlotContainer class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "WarehouseSlotContainer.h"
#include "Player.h"
#include <PacketStruct_DG.h>
#include "ItemManager.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CWarehouseSlotContainer::CWarehouseSlotContainer()
{
	Init( MAX_WAREHOUSE_SLOT_NUM, SI_WAREHOUSE );
	EndWarehouse();
	m_bChanged = FALSE;
}

CWarehouseSlotContainer::~CWarehouseSlotContainer()
{

}

VOID CWarehouseSlotContainer::ClearAll()
{
	SCItemSlotContainer::ClearAll();
	EndWarehouse();
	m_bChanged = FALSE;
}

BOOL CWarehouseSlotContainer::DepositMoney( MONEY PlusMoney ) 
{ 
	if( !IsStarted() ) return FALSE; 

	m_Money += PlusMoney; 

	m_bChanged = TRUE;

	return TRUE;
}

BOOL CWarehouseSlotContainer::WithDrawMoney( MONEY MinusMoney )
{
	if(!IsStarted()) return FALSE;

	if( m_Money < MinusMoney ) return FALSE;

	m_Money -= MinusMoney;

	m_bChanged = TRUE;

	return TRUE;
}

VOID CWarehouseSlotContainer::SerializeItemInfo( WAREHOUSE_TOTAL_INFO & ItemInfo, eSERIALIZE eType )
{
	if( eType == SERIALIZE_LOAD )
	{
		ItemInfo.m_Count = 0;
		ITEMSLOTEX * pSlot = ItemInfo.m_Slot;
		POSTYPE total = 0;
		for(POSTYPE i=0;i<GetMaxSlotNum();++i)
		{
			if( !IsEmpty(i) )
			{
				pSlot[total].m_ItemPos = i;
				SerializeItemStream(pSlot[total].m_ItemPos, &pSlot[total].m_Stream, eType);
				++total;
				++ItemInfo.m_Count;
			}
		}
	}
	else if( eType == SERIALIZE_STORE )
	{
		ASSERT( GetSlotNum() == 0 );
		ITEMSLOTEX * pSlot = ItemInfo.m_Slot;
		POSTYPE start = 0;
		POSTYPE total = ItemInfo.m_Count;
		for(POSTYPE i=start;i<total;++i)
		{
			SerializeItemStream(pSlot[i].m_ItemPos, &pSlot[i].m_Stream,eType);
			m_pPlayer->GetItemManager()->AddSerialCode( GetSlotIdx(), pSlot[i].m_Stream.ItemPart.dwSerial );
		}

		m_bChanged = FALSE;
	}
}

VOID CWarehouseSlotContainer::EndWarehouse()
{
	for(POSTYPE i=0;i<GetMaxSlotNum();++i)
	{
		if( !IsEmpty(i) )
		{
			m_pPlayer->GetItemManager()->RemoveSerialCode( GetSlot(i).GetSerial() );
		}
	}
	SetValidState(FALSE);
}

BOOL CWarehouseSlotContainer::SaveWarehouseToDBP( BOOL bEnd )
{
	if(!IsStarted()) return FALSE;
	if( !m_bChanged ) return TRUE;

	MSG_DG_CHARINFO_WAREHOUSE_CMD cmsg;
	cmsg.m_dwKey = m_pPlayer->GetUserKey();
	cmsg.m_WarehouseMoney = GetCurMoney();
	SerializeItemInfo( cmsg.m_ItemInfo, SERIALIZE_LOAD );
	BOOL rt = m_pPlayer->SendToGameDBPServer( &cmsg, cmsg.GetSize() );
	if( bEnd )
	{
		if( !rt ) return FALSE;

		MSG_DG_WAREHOUSE_END_SYN msg;
		msg.m_dwKey = m_pPlayer->GetUserKey();
		rt = m_pPlayer->SendToGameDBPServer( &msg, sizeof(msg) );
		if( rt ) m_bChanged = FALSE;
	}
	else
	{
		if( rt ) m_bChanged = FALSE;
	}

	return rt;
}


BOOL CWarehouseSlotContainer::InsertSlot( POSTYPE AtPos, SCSlot & IN rSlot )
{
	if( SCSlotContainer::InsertSlot( AtPos, rSlot ) )
	{
		m_pPlayer->GetItemManager()->AddSerialCode( GetSlotIdx(), rSlot.GetSerial() );
		m_bChanged = TRUE;
		return TRUE;
	}
	else 
		return FALSE;
}

VOID CWarehouseSlotContainer::DeleteSlot( POSTYPE AtPos, SCSlot * pSlotOut )
{
	m_pPlayer->GetItemManager()->RemoveSerialCode( GetSlot(AtPos).GetSerial() );
	SCSlotContainer::DeleteSlot( AtPos, pSlotOut );
	m_bChanged = TRUE;
}

VOID CWarehouseSlotContainer::UpdateSlot( POSTYPE AtPos, eSlotType type, DBSERIAL serial, SLOTCODE code, POSTYPE quickPos, DURATYPE dura, BYTE level, eUPDATE_BIT upbits )
{
	SCSlotContainer::UpdateSlot( AtPos, type, serial, code, quickPos, dura, level, upbits );
	m_bChanged = TRUE;
}

VOID CWarehouseSlotContainer::UpdateSlot( POSTYPE AtPos, SCSlot & IN rSlot )
{
	SCItemSlotContainer::UpdateSlot( AtPos, rSlot );
	m_bChanged = TRUE;
}